/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import View.Menu;
import Model.Const;
import Model.IntersectionInfo;

/**
 * Intersection main class for the traffic light simulation program.
 * @author Chiaki Sato
 * @since J2SDK 1.6
 * @version 1.0
 */
public class SimulatorCmd {

    /**
     * Public static main function to start and continue the program.
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Traffic oTraffic;
        int nExecCommand = Const.COMMAND_NONE;
        int nRoundMax = 10;
        String stResultDrawString = "";

        // create intersection information class object
        IntersectionInfo oIntersectionInfo = new IntersectionInfo();

        // create menu class object
        Menu oMenu = new Menu();

        // show main menu
        nExecCommand = oMenu.showMainMenu();

        // main menu loop
        while (nExecCommand != Const.COMMAND_EXIT_PROGRAM){

            // initialize intersection infomation as model data
            initIntersectionInfo(oMenu, oIntersectionInfo);

            // create traffic flow class object
            oTraffic = new Traffic(oIntersectionInfo);

            // sub menu loop
            while (nExecCommand != Const.COMMAND_RETURN_MAIN_MENU ){

                if (nExecCommand == Const.COMMAND_RUN_ONE_SIMULATION){

                        // execute single traffic flow
                        nRoundMax = 1;
                        stResultDrawString = oTraffic.doTrafficFlow(nRoundMax);
                        oMenu.drawIntersectionTraffic(stResultDrawString);
                }
                else if (nExecCommand == Const.COMMAND_RUN_CYCLES_SIMULATION){

                        // execute multiple traffic flows
                        nRoundMax = oMenu.getSimulationCycleCount();
                        stResultDrawString = oTraffic.doTrafficFlow(nRoundMax);
                        oMenu.drawIntersectionTraffic(stResultDrawString);
                }
                // show sub menu
                nExecCommand = oMenu.showSubMenu();

            }
            // show main menu
            nExecCommand = oMenu.showMainMenu();
        }
    }

    /**
     * initialize intersection information from menu as model data.
     * @param oMenu menu class object
     * @param oIntersectionInfo intersection information class object
     */
    public static void initIntersectionInfo(Menu oMenu, IntersectionInfo oIntersectionInfo){

        oIntersectionInfo.setLaneHoriz(oMenu.getLanesHoriz());
        oIntersectionInfo.setLaneVert(oMenu.getLanesVert());
        oIntersectionInfo.setProbabilityHoriz(oMenu.getProbabilityHoriz());
        oIntersectionInfo.setProbabilityVert(oMenu.getProbabilityVert());
        oIntersectionInfo.setSimulationCyleCount(oMenu.getSimulationCycleCount());
    }
}
